VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "TriangleSprite"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
' ************************************************
' Moving triangle sprite.
' ************************************************
Option Explicit

' The three corners are stored as the distance
' PtR(i) and angle PtT(i) from the center of the
' triangle (Cx, Cy) to the corner. This makes it
' easier to rotate the triangle. When rotated by
' angle Theta, the coordinates of corner i are:
'
'   x = Cx + PtR(i) * Cos(PtT(i) + Theta)
'   y = Cy + PtR(i) * Sin(PtT(i) + Theta)
'
Dim PtR(1 To 3) As Integer
Dim PtT(1 To 3) As Single
Dim Cx As Integer       ' Position of center.
Dim Cy As Integer
Dim Theta As Single     ' Orientation.
Dim Vx As Integer       ' Velocity.
Dim Vy As Integer
Dim Vtheta As Single    ' Angular velocity.
Dim Clr As Long         ' Color.
' ************************************************
' Draw the triangle on the indicated picture box.
' ************************************************
Public Sub DrawSprite(pic As PictureBox)
Dim i As Integer
Dim pts(1 To 3) As POINTAPI
Dim status As Long
Dim newpen As Long
Dim oldpen As Long
Dim newbrush As Long
Dim oldbrush As Long

    ' Compute the current corner locations.
    For i = 1 To 3
        pts(i).x = Cx + PtR(i) * Cos(PtT(i) + Theta)
        pts(i).y = Cy + PtR(i) * Sin(PtT(i) + Theta)
    Next i

    newpen = CreatePen(PS_SOLID, 0, Clr)
    oldpen = SelectObject(pic.hdc, newpen)
    newbrush = CreateSolidBrush(Clr)
    oldbrush = SelectObject(pic.hdc, newbrush)
    
    status = Polygon(pic.hdc, pts(1), 3)

    newpen = SelectObject(pic.hdc, oldpen)
    status = DeleteObject(newpen)
    newbrush = SelectObject(pic.hdc, oldbrush)
    status = DeleteObject(newbrush)
End Sub

' ************************************************
' Initialize the rectangle.
' ************************************************
Public Sub InitializeSprite(x As Integer, y As Integer, dx As Integer, dy As Integer, r1 As Integer, t1 As Integer, r2 As Integer, t2 As Integer, r3 As Integer, t3 As Integer, dangle As Single, c As Long)
    PtR(1) = r1
    PtT(1) = t1
    
    PtR(2) = r2
    PtT(2) = t2
    
    PtR(3) = r3
    PtT(3) = t3
    
    Cx = x
    Cy = y
    Theta = 0
    Vx = dx
    Vy = dy
    Vtheta = dangle
    Clr = c
End Sub

' ************************************************
' Add the velocity components to the sprite's
' position components.
' ************************************************
Public Sub MoveSprite(xmax As Integer, ymax As Integer)
    Cx = Cx + Vx
    Cy = Cy + Vy
    
    If Cx < 0 Then
        Cx = -Cx
        Vx = -Vx
    ElseIf Cx > xmax Then
        Cx = 2 * xmax - Cx
        Vx = -Vx
    End If
    If Cy < 0 Then
        Cy = -Cy
        Vy = -Vy
    ElseIf Cy > ymax Then
        Cy = 2 * ymax - Cy
        Vy = -Vy
    End If
    
    Theta = Theta + Vtheta
End Sub


